package com.ruoyi.controller.basic;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.basic.MesWorkshop;
import com.ruoyi.service.basic.IMesWorkshopService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车间信息Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/mes/workshop")
public class MesWorkshopController extends BaseController {
    @Autowired
    private IMesWorkshopService mesWorkshopService;

    /**
     * 查询车间信息列表
     */
    @PreAuthorize("@ss.hasPermi('mes:workshop:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesWorkshop mesWorkshop) {
        startPage();
        List<MesWorkshop> list = mesWorkshopService.selectMesWorkshopList(mesWorkshop);
        return getDataTable(list);
    }

    /**
     * 导出车间信息列表
     */
    @PreAuthorize("@ss.hasPermi('mes:workshop:export')")
    @Log(title = "车间信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesWorkshop mesWorkshop) {
        List<MesWorkshop> list = mesWorkshopService.selectMesWorkshopList(mesWorkshop);
        ExcelUtil<MesWorkshop> util = new ExcelUtil<MesWorkshop>(MesWorkshop.class);
        util.exportExcel(response, list, "车间信息数据");
    }

    /**
     * 获取车间信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('mes:workshop:query')")
    @GetMapping(value = "/{workshopId}")
    public AjaxResult getInfo(@PathVariable("workshopId") Long workshopId) {
        return success(mesWorkshopService.selectMesWorkshopByWorkshopId(workshopId));
    }

    /**
     * 新增车间信息
     */
    @PreAuthorize("@ss.hasPermi('mes:workshop:add')")
    @Log(title = "车间信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesWorkshop mesWorkshop) {
        return toAjax(mesWorkshopService.insertMesWorkshop(mesWorkshop));
    }

    /**
     * 修改车间信息
     */
    @PreAuthorize("@ss.hasPermi('mes:workshop:edit')")
    @Log(title = "车间信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesWorkshop mesWorkshop) {
        return toAjax(mesWorkshopService.updateMesWorkshop(mesWorkshop));
    }

    /**
     * 删除车间信息
     */
    @PreAuthorize("@ss.hasPermi('mes:workshop:remove')")
    @Log(title = "车间信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workshopIds}")
    public AjaxResult remove(@PathVariable Long[] workshopIds) {
        return toAjax(mesWorkshopService.deleteMesWorkshopByWorkshopIds(workshopIds));
    }

    /**
     * 校验车间编码
     */
    @PostMapping("/checkWorkshopCodeUnique")
    public AjaxResult checkWorkshopCodeUnique(@RequestBody MesWorkshop mesWorkshop) {
        String result = mesWorkshopService.checkWorkshopCodeUnique(mesWorkshop);
        if ("0".equals(result)) {
            return success();
        }
        return error("车间编码已存在");
    }

    /**
     * 查询启用状态的车间列表
     */
    @GetMapping("/getActiveList")
    public AjaxResult getActiveList() {
        List<MesWorkshop> list = mesWorkshopService.selectActiveWorkshopList();
        return success(list);
    }
}